//
// Created by jsjxsy on 2022/7/6.
// 数据的类型与printf格式的类型一定要匹配，否则显示出问题
//
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <io_utils.h>
#define SIZE 5
double average(double array[]) {
  double sum = 0;
  for (int i = 0; i < SIZE; ++i) {
    sum += array[i];
  }
  double result = sum / SIZE;
  PRINT_DOUBLE(result);
  return result;
}

double averageTotal(double array1[], double array2[], double array3[]) {
  double sum = 0;
  for (int i = 0; i < 3; ++i) {
    for (int j = 0; j < SIZE; ++j) {
      if (i == 0) {
        sum += array1[j];
      } else if (i == 1) {
        sum += array2[j];
      } else {
        sum += array3[j];
      }
    }
  }
  return (sum / (3 * SIZE));
}

double max(double array1[], double array2[], double array3[]) {
  double max = 0;
  for (int i = 0; i < 3; ++i) {
    for (int j = 0; j < SIZE; ++j) {
      if (i == 0) {
        if (max < array1[j]) {
          max = array1[j];
        }
      } else if (i == 1) {
        if (max < array2[j]) {
          max = array2[j];
        }
      } else {
        if (max < array3[j]) {
          max = array3[j];
        }
      }
    }
  }
  return max;
}

int main(void) {
  puts("please input 3 array, 5 double number in every one array:");
  char *input;
  gets(input);
  puts(input);
  char *buf[15], *p = NULL;
  p = strtok(input, ",");
  int i = 0;
  while (p) {
    buf[i] = p;
    i++;
    p = strtok(NULL, ",");
  }
  double array1[SIZE] = {0};
  double array2[SIZE] = {0};
  double array3[SIZE] = {0};
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < SIZE; ++j) {
      if (i == 0) {
        array1[j] = strtod(buf[i * SIZE + j], NULL);
      } else if (i == 1) {
        array2[j] = strtod(buf[i * SIZE + j], NULL);
      } else {
        array3[j] = strtod(buf[i * SIZE + j], NULL);
      }
    }
  }
  PRINT_DOUBLE_ARRAY(array1, SIZE);
  PRINT_DOUBLE_ARRAY(array2, SIZE);
  PRINT_DOUBLE_ARRAY(array3, SIZE);

  double result1 = average(array1);
  PRINT_DOUBLE(result1);
  double result2 = average(array2);
  PRINT_DOUBLE(result2);
  double result3 = average(array3);
  PRINT_DOUBLE(result3);

  double result = averageTotal(array1, array2, array3);
  PRINT_DOUBLE(result);
  double maxResult = max(array1, array2, array3);
  PRINT_DOUBLE(maxResult);
  return 0;
}